// https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/description/

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int n = nums.size();
        int k = 0;
        for (auto s : nums) {
            k += s;
        }
        k -= x;
        if (k < 0)
            return -1;
        int len = -0x3f3f3f3f;
        int sum = 0;
        for (int i = 0, j = 0; j < n; j++) {
            sum += nums[j];
            while (sum > k) // 进窗口
            {
                sum -= nums[i++];
            }
            if (sum == k) // 出窗口
            {
                len = max(len, j - i + 1);
            }
        }
        return n - len >= 0x3f3f3f3f ? -1 : n - len;
    }
};